package com.sunbeam.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @Title: XmlUtils.java
 * @Package com.sunbeam.utils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2014/02/11 10:30:12
 * @version V1.0
 */
public class XmlUtils {

	private static String filePath;
	static{
		filePath=XmlUtils.class.getClassLoader().getResource("users.xml").getFile();
		System.out.println(filePath);
	}
	
	public static Document getDocument() throws DocumentException{
		SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File(filePath));
		return document;
	}
	
	public static void write2Xml(Document document) throws IOException{
		// Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter( new FileWriter( filePath ), format );
        writer.write( document );
        
        writer.close();
	}
}

